#导入模块
import pygame
from pygame.locals import *  #使用pygame中的常量
import time,random

class HeroPlane:
	'''玩家飞机（英雄）'''
	def __init__(self,screen_temp):
		self.x = 235
		self.y = 525
		self.screen = screen_temp
		self.image = pygame.image.load("./images/wj.PNG")  #加载玩家飞机图片
		self.bullet_list = []   #存放玩家飞机的子弹列表

	def display(self):
		'''绘制玩家飞机'''
		#绘制子弹
		for b in self.bullet_list:
			b.display()   
			if b.hero_move():
				self.bullet_list.remove(b)
		self.screen.blit(self.image,(self.x,self.y))

	def move(self,key):
		'''控制玩家飞机的移动，通过传入的key值判断移动方向'''
		if key == "left":  #左移
			self.x -= 6
			if self.x<=0:
				self.x = 0
		elif key == "right":  #右移
			self.x += 6
			if self.x>=470:
				self.x = 470
		if key == "up":   #向上移动
			self.y -= 5
			if self.y<=0:
				self.y = 0
		elif key == "down":   #向下移动
			self.y += 5
			if self.y>=610:
				self.y=610

	def fire(self):
		'''发射子弹'''
		self.bullet_list.append(Bullet(self.screen,self.x,self.y))

	def bang(self):
		'''实现玩家飞机与敌机子弹的碰撞检测'''
		i = 0
		while i<4:
			self.image = pygame.image.load("./images/bz"+str(i)+".PNG")
			self.screen.blit(self.image,(self.x+20,self.y+10))
			i += 1
			
class Bullet:
	'''子弹类'''
	def __init__(self,screen_temp,x,y):
		self.x = x+41
		self.y = y
		self.screen = screen_temp
		self.image = pygame.image.load("./images/zd.PNG")

	def display(self):
		'''绘制子弹'''
		self.screen.blit(self.image,(self.x,self.y))

	def hero_move(self):
		'''玩家飞机的子弹移动'''
		self.y -= 10
		if self.y<=-20:
			return True

	def enemy_move(self):
		'''敌机的子弹移动'''
		self.y += 7
		if self.y>=680:  #子弹出屏
			return True

class EnemyPlane:
	'''敌机类'''
	def __init__(self,screen_temp):
		self.x = random.choice(range(470))
		self.y = -75
		self.screen = screen_temp
		self.image = pygame.image.load("./images/dj"+str(random.choice(range(3))+1)+".PNG")
		self.bullet_list = []
		self.bang_list = []

	def display(self,hero):
		'''绘制敌机和大家发射的子弹'''
		for b in self.bullet_list:
			b.display()
			if b.enemy_move():
				self.bullet_list.remove(b)
		self.screen.blit(self.image,(self.x,self.y))

	def fire(self):
		self.bullet_list.append(Bullet(self.screen,self.x,self.y))

	def move(self,hero):
		'''敌机移动'''
		self.y += 2
		if self.y>680:  #敌机出屏
			return True
		#遍历所有子弹，并执行碰撞检测
		for bo in hero.bullet_list:
			if bo.x>self.x+25 and bo.x<self.x+75 and bo.y>self.y+10 and bo.y<self.y+50:
				hero.bullet_list.remove(bo)
				i = 0
				#加载爆炸画面效果
				while i<4:
					self.image = pygame.image.load("./images/bz"+str(i)+".PNG")
					self.screen.blit(self.image,(self.x+20,self.y+10))
					i += 1
				return True

def key_control(hero_temp):
	'''键盘控制方法'''
	#退出操作
	for event in pygame.event.get():
		if event.type == QUIT:
			exit()
	#监控并获得键盘操作
	pressed_keys = pygame.key.get_pressed()  
	#做出判断，并执行相应的操作
	if pressed_keys[K_LEFT] or pressed_keys[K_a]:
		hero_temp.move("left")
	elif pressed_keys[K_RIGHT] or pressed_keys[K_d]:
		hero_temp.move("right")
	if pressed_keys[K_UP] or pressed_keys[K_w]:
		hero_temp.move("up")
	elif pressed_keys[K_DOWN] or pressed_keys[K_s]:
		hero_temp.move("down")
	if pressed_keys[K_SPACE]:
		hero_temp.fire()

def main():
	'''主程序函数'''
	#创建游戏窗口
	screen = pygame.display.set_mode((560,670),0,0)
	#创建游戏背景
	background = pygame.image.load("./images/bj.PNG")
	#创建玩家飞机（英雄）
	hero = HeroPlane(screen)
	m = -727
	enemylist = []  #存放敌机的列表
	while True:
		screen.blit(background,(0,m))
		m+=1
		if m>=-64:
			m = -727
		hero.display()   #绘制玩家飞机绘制
		#执行键盘控制	
		key_control(hero)
		#随机绘制敌机	
		if random.choice(range(50)) == 10:
			enemylist.append(EnemyPlane(screen))
		# 遍历敌机并绘制移动
		for em in enemylist:
			em.display(hero)
			if random.choice(range(150)) == 10:
				em.fire()		
			if em.move(hero):
				enemylist.remove(em)
			# 敌机子弹与玩家飞机的碰撞
			for bo in em.bullet_list:
				if bo.x > hero.x+25 and bo.x < hero.x+75 and bo.y > hero.y+10 and bo.y < hero.y+50:
					em.bullet_list.remove(bo)
					hero.bang()
		# 更新显示
		pygame.display.update()
		# 定时显示
		time.sleep(0.04)
# 判断当前是否是主运行程序，并调用
if __name__ == "__main__":
	main()